package de.hsmannheim.poolraumapp.fragment;

import java.util.Set;

import com.google.common.collect.BiMap;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.GridLayout;
import android.widget.RadioButton;
import de.hsmannheim.poolraumapp.R;
import de.hsmannheim.poolraumapp.activity.DetailActivity;
import de.hsmannheim.poolraumapp.activity.NavigationActivity;
import de.hsmannheim.poolraumapp.model.RoomQuery;
import de.hsmannheim.poolraumapp.model.RoomQuery.What;
import de.hsmannheim.poolraumapp.model.RoomQuery.WhatOS;
import de.hsmannheim.poolraumapp.model.RoomQuery.Where;

/**
 * A fragment representing a single Item detail screen. This fragment is either
 * contained in a {@link NavigationActivity} in two-pane mode (on tablets) or a
 * {@link DetailActivity} on handsets.
 */
public class WhatFragment extends PoolraumFragment implements OnCheckedChangeListener {
	private static final String TAG = WhatFragment.class.getName(); 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_what, container, false);
		
		RoomQuery roomQuery = getRoomQuery();
		
		for (WhatOS os : WhatOS.values()) {
			CheckBox checkBox = new CheckBox(getActivity());
			checkBox.setText(getResources().getIdentifier("what_os_" + os.name(), "string", getActivity().getPackageName()));
			checkBox.setTag(os);
			checkBox.setChecked(roomQuery.getOS().contains(os));
			checkBox.setOnCheckedChangeListener(this);
			((ViewGroup)view).addView(checkBox);
		}
		
		setChecked(view, getId(roomQuery.getWhat()));
		
		((RadioButton)view.findViewById(R.id.what_any_of)).setOnCheckedChangeListener(this);
		((RadioButton)view.findViewById(R.id.what_all_of)).setOnCheckedChangeListener(this);
		return view;
	}
	
	private int getId(What where) {
		switch(where) {
			case ANY_OF:
				return R.id.what_any_of;
			case ALL_OF:
				return R.id.what_all_of;
		}
		throw new RuntimeException();
	}
	
	private void setChecked(View view, int id) {
		Log.d(TAG, "Enabling and disabling what views");
		((RadioButton)view.findViewById(R.id.what_any_of)).setChecked(id == R.id.what_any_of);
		((RadioButton)view.findViewById(R.id.what_all_of)).setChecked(id == R.id.what_all_of);
		Log.d(TAG, "What views enabled and disabled");
	}
	
	@Override
	public void onCheckedChanged(CompoundButton button, boolean checked) {
		RoomQuery roomQuery = getRoomQuery();			
		switch (button.getId()) {
		case R.id.what_any_of:
			if (checked) {
				roomQuery.setWhat(What.ANY_OF);
				setChecked(getView(), button.getId());
			}
			break;
		case R.id.what_all_of:
			if (checked) {
				roomQuery.setWhat(What.ALL_OF);
				setChecked(getView(), button.getId());
			}
			break;
		default:
			Set<WhatOS> os = roomQuery.getOS();
			if (checked) {
				os.add((WhatOS)button.getTag());
			}
			else {
				os.remove((WhatOS)button.getTag());
			}
			roomQuery.setOS(os);
		}
	}
}
